//
//  MWLaunchController.m
//  XiRan-iOS
//
//  Created by liuyiming on 2019/12/21.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "AppDelegate.h"
#import "MWLaunchController.h"

#import "MWXRTabBarController.h"

#import "HSFTabBarController.h"

#import "MWNavigationController.h"
#import "MWHomeController.h"
#import "ShowRoomViewController.h"
#import "MWShopController.h"
#import "MWTreeSeedController.h"
#import "MWApplicationController.h"
#import "MWMineController.h"

#define kIs_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define kIs_iPhoneX kScreenWidth >=375.0f && kScreenHeight >=812.0f&& kIs_iphone
/*TabBar高度*/
#define kTabBarHeight (CGFloat)(kIs_iPhoneX?(49.0 + 34.0):(49.0))


@interface MWLaunchController ()

@property(nonatomic, strong) UIImageView *launchIV;

@property(nonatomic, strong) dispatch_group_t group;

@end

@implementation MWLaunchController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _launchIV = [[UIImageView alloc] init];
    _launchIV.contentMode = UIViewContentModeScaleAspectFill;
    _launchIV.image = [UIImage imageNamed:@"launch_en"];
    [self.view addSubview:_launchIV];
    
    [_launchIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    if ([[MWConfigManager manager].language isEqualToString:@"0"]) {
        _launchIV.image = [UIImage imageNamed:@"launch_ch"];
    } else if ([[MWConfigManager manager].language isEqualToString:@"2"]) {
        _launchIV.image = [UIImage imageNamed:@"launch_ja"];
    } else {
        _launchIV.image = [UIImage imageNamed:@"launch_en"];
    }
    
    AppDelegate *ad = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (ad.reachabilityStatus > 0) {
        [self loadConfigInfo];
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)networkChange:(NSNotification *)notification {
    AppDelegate *ad = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (ad.isNeedRequest && ad.reachabilityStatus > 0) {
        [self loadConfigInfo];
    }
}
- (void)loadConfigInfo {
    _group = dispatch_group_create();
    dispatch_group_enter(_group);
    [self loadLocalizableConfig];
       
    dispatch_group_enter(_group);
    [self loadVisitorInfo];
       
    dispatch_group_notify(_group, dispatch_get_main_queue(), ^{
        if ([[MWConfigManager manager].productType isEqualToString:@"0"]) {
            NSString *homeTitle = [[MWConfigManager manager] objectForKey:@"homepage" defaultValue:@"首页"];
            NSString *showroomTitle = [[MWConfigManager manager] objectForKey:@"showroom" defaultValue:@"展厅"];
            NSString *shopTitle = [[MWConfigManager manager] objectForKey:@"goods" defaultValue:@"商品"];
            NSString *treeSeedTitle = [[MWConfigManager manager] objectForKey:@"treetype" defaultValue:@"树种"];
            NSString *applicationTitle = [[MWConfigManager manager] objectForKey:@"application" defaultValue:@"应用"];
            NSString *mineTitle = [[MWConfigManager manager] objectForKey:@"my" defaultValue:@"我的"];

            MWNavigationController *navi1 = [[MWNavigationController alloc] initWithRootViewController:[[MWHomeController alloc] init]];
            MWNavigationController *navi2 = [[MWNavigationController alloc] initWithRootViewController:[[ShowRoomViewController alloc] init]];
            MWNavigationController *navi3 = [[MWNavigationController alloc] initWithRootViewController:[[MWShopController alloc] init]];
            MWNavigationController *navi4 = [[MWNavigationController alloc] initWithRootViewController:[[MWTreeSeedController alloc] init]];
            MWNavigationController *navi5 = [[MWNavigationController alloc] initWithRootViewController:[[MWApplicationController alloc] init]];
            MWNavigationController *navi6 = [[MWNavigationController alloc] initWithRootViewController:[[MWMineController alloc] init]];
            HSFTabBarController *rootController = [[HSFTabBarController alloc] init];
            rootController.childVCArr = @[navi1, navi2, navi3, navi5, navi4, navi6];
            rootController.source = @[@{@"title":homeTitle, @"selImg":@"tab_home_s", @"norImg":@"tab_home_n"},@{@"title":showroomTitle, @"selImg":@"tab_showroom_s", @"norImg":@"tab_showroom_n"},
            @{@"title":shopTitle, @"selImg":@"tab_shop_s", @"norImg":@"tab_shop_n"},
                        @{@"title":applicationTitle, @"selImg":@"tab_application_s", @"norImg":@"tab_application_n"},
                                      @{@"title":treeSeedTitle, @"selImg":@"tab_tree_seed_s", @"norImg":@"tab_tree_seed_n"},

            @{@"title":mineTitle, @"selImg":@"tab_mine_s", @"norImg":@"tab_mine_n"}];
            rootController.norColor = MWHexColor(@"#666666");
            rootController.selColor = MWHexColor(@"#333333");
            rootController.loadAll = YES;//是否一次全部加载
            rootController.canScroll = NO;
            rootController.tabBarHeight = kTabBarHeight;
            rootController.style = HSFTabBarStyle_baseline;
            rootController.baseline_color = MWHexColor(@"#333333");
            rootController.isHaveTopline = NO;
            [rootController setUp];
            UIApplication *application = [UIApplication sharedApplication];
            application.keyWindow.rootViewController = rootController;
            [MWUserManager manager].tbc = rootController;
        } else {
            MWXRTabBarController *rootController = [[MWXRTabBarController alloc] init];
            UIApplication *application = [UIApplication sharedApplication];
            application.keyWindow.rootViewController = rootController;
        }
    });
}

#pragma mark - 未登录时访客需要
- (void)loadVisitorInfo {
    // 已登录，不在加载
    if ([MWUserManager manager].isLogin) {
        dispatch_group_leave(self.group);
        return;
    }
    
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    [paraDic setObject:[MWConfigManager manager].language forKey:@"language"];
    
    [[MWNetworkManager manager] POST:kVsitorLoginUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
//        NSString *msg = [dic objectForKey:@"msg"];
        if (code == 1) {
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            MWUser *user = [MWUser mj_objectWithKeyValues:[dataDic objectForKey:@"userinfo"]];
            [MWUserManager manager].user = user;
        }
        dispatch_group_leave(self.group);
    } failure:^(NSError *error) {
//        dispatch_group_leave(self.group);
    }];
}

#pragma mark - 国际化配置常量
- (void)loadLocalizableConfig {
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    if ([[MWConfigManager manager].language isEqualToString:@"0"]) {
        [paraDic setObject:@"zh" forKey:@"lang"];
    } else if ([[MWConfigManager manager].language isEqualToString:@"2"]) {
        [paraDic setObject:@"ja" forKey:@"lang"];
    } else {
        [paraDic setObject:@"en" forKey:@"lang"];
    }
    
    [[MWNetworkManager manager] POST:kLocalizableConfigUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
//        NSString *msg = [dic objectForKey:@"msg"];
        if (code == 1) {
            [MWConfigManager manager].localizableDic = [dic objectForKey:@"data"];
            [[MWConfigManager manager] saveConfig];
            [MWLocalizableManager configCopywriter];
        }
        dispatch_group_leave(self.group);
        
    } failure:^(NSError *error) {
//        dispatch_group_leave(self.group);
    }];
}



@end
